package com.thwj.project.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thwj.common.exception.ServiceException;
import com.thwj.common.utils.StringUtils;
import com.thwj.framework.mybatis_plus.GeoUtil;
import com.thwj.project.app.event.UserMessageEvent;
import com.thwj.project.biz.domain.Merchant;
import com.thwj.project.biz.domain.MerchantApply;
import com.thwj.project.biz.domain.User;
import com.thwj.project.biz.enums.AuditStatusEnum;
import com.thwj.project.biz.enums.MerchantStatusEnum;
import com.thwj.project.biz.enums.MessageTypeEnum;
import com.thwj.project.biz.mapper.MerchantApplyMapper;
import com.thwj.project.biz.service.IMerchantApplyService;
import com.thwj.project.biz.service.IMerchantService;
import com.thwj.project.biz.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

/**
 * 商家Service业务层处理
 *
 * @author generate
 * @date 2023-07-13
 */
@Service
public class MerchantApplyServiceImpl extends ServiceImpl<MerchantApplyMapper, MerchantApply> implements IMerchantApplyService {

    @Autowired
    IUserService userService;
    @Autowired
    IMerchantService merchantService;
    @Autowired
    ApplicationEventPublisher applicationEventPublisher;

    @Override
    public int auditMerchantApply(MerchantApply current) {
        MerchantApply merchantApply = this.getById(current.getId());
        // 审核前后状态没有改变 不做其他调整
        if (merchantApply.getStatus() == current.getStatus()) {
            return 0;
        }
        if (StringUtils.isEmpty(current.getRemark()) && AuditStatusEnum.auditing.getValue() != current.getStatus()) {
            throw new ServiceException("审核不通过，必须填写备注，备注将会通知给用户");
        }
        User user = userService.getById(current.getUserId());

        AuditStatusEnum statusEnum = AuditStatusEnum.getEnumByValue(current.getStatus());
        if (AuditStatusEnum.auditing_success.getValue() == statusEnum.getValue()) {
            // 读取当前用户是否存在正式商家
            Merchant merchant = merchantService.getOne(new QueryWrapper<Merchant>().eq("user_id", current.getUserId()),false);
            Merchant newMerchant = new Merchant();
            if (merchant == null) {
                BeanUtils.copyProperties(merchantApply, newMerchant);
                newMerchant.setId(null);
                newMerchant.setLocation(GeoUtil.getWgs84Point(current.getLatitude(),current.getLongitude()));
                newMerchant.setStatus(MerchantStatusEnum.up.getValue());
                newMerchant.setTagJson(merchantApply.getTagIdList());
                merchantService.save(newMerchant);
            } else {
                BeanUtils.copyProperties(merchantApply, newMerchant);
                newMerchant.setId(merchant.getId());
                newMerchant.setLocation(GeoUtil.getWgs84Point(current.getLatitude(),current.getLongitude()));
                newMerchant.setStatus(merchant.getStatus());
                newMerchant.setTagJson(merchantApply.getTagIdList());
                merchantService.updateById(newMerchant);
            }
        }
        this.updateById(current);

//        applicationEventPublisher.publishEvent(
//                WxMpMessageEvent.builder()
//                        .userId(current.getUserId())
//                        .miniAppPath("/pages/shop-info/shop-info")
//                        .pushTemplateEnum(PushTemplateEnum.AUTHTHENTICATION_AUDITING)
//                        .templateDataList(
//                                PushTemplateEnum.getAUTHTHENTICATION_AUDITING_Data(
//                                        current.getName(),
//                                        user.getUserNickName(),
//                                        statusEnum.getName() + "" + merchantApply.getRemark(),
//                                        new Date())
//                        ).build()
//        );

        applicationEventPublisher.publishEvent(UserMessageEvent.builder()
                .userId(current.getUserId())
                .type(MessageTypeEnum.MERCHANT_AUDIT_MESSAGE)
                .withContent("商家认证审核通知", "您提交的审核已处理").build());


        return 1;
    }
}
